<?php

namespace App\Wechat;

use EasyWeChat\Kernel\Contracts\AccessToken as AccessTokenInterface;
use EasyWeChat\MiniApp\AccessToken;
use Illuminate\Cache\RedisTaggedCache;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Http;

class MiniAccessToken implements AccessTokenInterface
{
    public function getToken(): string
    {
        $config = config('easywechat.mini_app.default');
        $key = sprintf('official_account.access_token.%s.%s', $config['app_id'],$config['secret']);
        $taggedCache = Cache::tags(['access']);
        $accessToken = new AccessToken($config['app_id'],$config['secret'],$key,$taggedCache);
        $accessToken->refresh();
        return $accessToken->getToken();
    }

    public function toQuery(): array
    {
        return ['access_token' => $this->getToken()];
    }
}
